﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TarreOA.TarreOAModels;
using TarreOA.TarreOADAL;

namespace TarreOA.TarreOABLL
{
    public class FlowFront_CanCreateWork_Service
    {
        /// <summary>
        /// 根据当前用户的登录名查找该用户可以新建的工作
        /// </summary>
        /// <param name="curLoginUser"></param>
        /// <returns></returns>
        public List<FlowFront_CanCreateWork> FindByUserLoginName(User curLoginUser)
        {
            List<FlowFront_CanCreateWork> list = null;
            SqlConnection sqlConn = SQLManager.GetConn();
            try
            {
                FlowFront_CanCreateWork_DAO f_c_Dao = new FlowFront_CanCreateWork_DAO();
                string sql = "select * from Flow_Type where [id] in (select Base_Flow_Id from Flow_NewFlow where [id] in (select New_flow_id from Flow_newFlowStep where sort_id=1 and (Prc_users like '%,"
                    + curLoginUser.LoginName + ",%' or Prc_Users like '" + curLoginUser.LoginName + ",%' or Prc_Sections like '%," + curLoginUser.Section.SectionId + ",%' or Prc_Sections like '"
                    + curLoginUser.Section.SectionId + ",%' or Prc_Roles like '%," + curLoginUser.PopedomGroup.PopedomGroupId + ",%' or Prc_Roles like '" + curLoginUser.PopedomGroup.PopedomGroupId + ",%')))";
                SqlDataReader sqlDr = f_c_Dao.FindByTest(sql, new Hashtable(), sqlConn);
                List<Flow_Type> list_type = null;
                if (sqlDr != null && sqlDr.HasRows)
                {
                    try
                    {
                        list = new List<FlowFront_CanCreateWork>();
                        list_type = new List<Flow_Type>();
                        while (sqlDr.Read())
                        {
                            Flow_Type ft = new Flow_Type();
                            ft.Id = (int)sqlDr["Id"];
                            ft.Sort_id = (int)sqlDr["Flow_Sort"];
                            ft.Flow_name = (string)sqlDr["Flow_name"];
                            list_type.Add(ft);
                        }
                    }
                    finally
                    {
                        sqlDr.Close();
                        sqlDr.Dispose();
                    }
                }
                sql = "select * from Flow_NewFlow where [id] in (select New_flow_id from Flow_newFlowStep where sort_id = 1 and (Prc_users like '%,"
                    + curLoginUser.LoginName + ",%' or Prc_Users like '" + curLoginUser.LoginName + ",%' or Prc_Sections like '%," + curLoginUser.Section.SectionId + ",%' or Prc_Sections like '"
                    + curLoginUser.Section.SectionId + ",%' or Prc_Roles like '%," + curLoginUser.PopedomGroup.PopedomGroupId + ",%' or Prc_Roles like '" + curLoginUser.PopedomGroup.PopedomGroupId + ",%'))";
                sqlDr = f_c_Dao.FindByTest(sql, new Hashtable(), sqlConn);
                List<Flow_NewFlow> list_newflow = null;
                if (sqlDr != null && sqlDr.HasRows)
                {
                    try
                    {
                        list_newflow = new List<Flow_NewFlow>();
                        while (sqlDr.Read())
                        {
                            Flow_NewFlow fnf = new Flow_NewFlow();
                            fnf.NewFlowl_id = (int)sqlDr["id"];
                            fnf.NewFlow_sort = (int)sqlDr["New_Flow_Sort"];
                            fnf.NewFlow_type = (int)sqlDr["New_Flow_Type"];
                            fnf.NewFlow_name = (string)sqlDr["New_Flow_Name"];
                            fnf.Base_flow_id = (int)sqlDr["Base_Flow_Id"];
                            list_newflow.Add(fnf);
                        }
                    }
                    finally
                    {
                        sqlDr.Close();
                        sqlDr.Dispose();
                    }
                }
                if (list_type != null && list_newflow != null)
                {
                    foreach (Flow_Type ft in list_type)
                    {
                        List<Flow_NewFlow> list_temp = new List<Flow_NewFlow>();
                        FlowFront_CanCreateWork ff_cc = new FlowFront_CanCreateWork();
                        ff_cc.Flow_type_id = ft.Id;
                        ff_cc.Flow_type_name = ft.Flow_name;
                        foreach (Flow_NewFlow fnf in list_newflow)
                        {
                            if (fnf.Base_flow_id == ft.Id)
                            {
                                list_temp.Add(fnf);
                            }
                        }
                        ff_cc.Flow_newflows = list_temp;
                        list.Add(ff_cc);
                    }
                }
            }
            finally
            {
                if (sqlConn != null)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return list;
        }

        /// <summary>
        /// 根据用户查找用户可以查询统计的流程分类
        /// </summary>
        /// <param name="curLoginUser"></param>
        /// <returns></returns>
        public List<FlowFront_CanCreateWork> FindForStat(User curLoginUser)
        {
            List<FlowFront_CanCreateWork> list = null;
            SqlConnection sqlConn = SQLManager.GetConn();
            string sql = "";
            try
            {
                FlowFront_CanCreateWork_DAO f_c_Dao = new FlowFront_CanCreateWork_DAO();
                if (curLoginUser.PopedomGroup.PopedomGroupId == 1)
                {
                    sql = "select * from Flow_Type";
                }
                else
                {
                    sql = "select * from Flow_Type where [id] in (select Base_Flow_Id from Flow_NewFlow where [id] in (select New_flow_id from Flow_newFlowStep where Prc_users like '%,"
                        + curLoginUser.LoginName + ",%' or Prc_Users like '" + curLoginUser.LoginName + ",%' or Prc_Sections like '%," + curLoginUser.Section.SectionId + ",%' or Prc_Sections like '"
                        + curLoginUser.Section.SectionId + ",%' or Prc_Roles like '%," + curLoginUser.PopedomGroup.PopedomGroupId + ",%' or Prc_Roles like '" + curLoginUser.PopedomGroup.PopedomGroupId + ",%'))";
                }
                SqlDataReader sqlDr = f_c_Dao.FindByTest(sql, new Hashtable(), sqlConn);
                List<Flow_Type> list_type = null;
                if (sqlDr != null && sqlDr.HasRows)
                {
                    try
                    {
                        list = new List<FlowFront_CanCreateWork>();
                        list_type = new List<Flow_Type>();
                        while (sqlDr.Read())
                        {
                            Flow_Type ft = new Flow_Type();
                            ft.Id = (int)sqlDr["Id"];
                            ft.Sort_id = (int)sqlDr["Flow_Sort"];
                            ft.Flow_name = (string)sqlDr["Flow_name"];
                            list_type.Add(ft);
                        }
                    }
                    finally
                    {
                        sqlDr.Close();
                        sqlDr.Dispose();
                    }
                }
                if (curLoginUser.PopedomGroup.PopedomGroupId == 1)
                {
                    sql = "select * from Flow_NewFlow";
                }
                else
                {
                    sql = "select * from Flow_NewFlow where [id] in (select New_flow_id from Flow_newFlowStep where Prc_users like '%,"
                        + curLoginUser.LoginName + ",%' or Prc_Users like '" + curLoginUser.LoginName + ",%' or Prc_Sections like '%," + curLoginUser.Section.SectionId + ",%' or Prc_Sections like '"
                        + curLoginUser.Section.SectionId + ",%' or Prc_Roles like '%," + curLoginUser.PopedomGroup.PopedomGroupId + ",%' or Prc_Roles like '" + curLoginUser.PopedomGroup.PopedomGroupId + ",%')";
                }
                sqlDr = f_c_Dao.FindByTest(sql, new Hashtable(), sqlConn);
                List<Flow_NewFlow> list_newflow = null;
                if (sqlDr != null && sqlDr.HasRows)
                {
                    try
                    {
                        list_newflow = new List<Flow_NewFlow>();
                        while (sqlDr.Read())
                        {
                            Flow_NewFlow fnf = new Flow_NewFlow();
                            fnf.NewFlowl_id = (int)sqlDr["id"];
                            fnf.NewFlow_sort = (int)sqlDr["New_Flow_Sort"];
                            fnf.NewFlow_type = (int)sqlDr["New_Flow_Type"];
                            fnf.NewFlow_name = (string)sqlDr["New_Flow_Name"];
                            fnf.Base_flow_id = (int)sqlDr["Base_Flow_Id"];
                            list_newflow.Add(fnf);
                        }
                    }
                    finally
                    {
                        sqlDr.Close();
                        sqlDr.Dispose();
                    }
                }
                if (list_type != null && list_newflow != null)
                {
                    foreach (Flow_Type ft in list_type)
                    {
                        List<Flow_NewFlow> list_temp = new List<Flow_NewFlow>();
                        FlowFront_CanCreateWork ff_cc = new FlowFront_CanCreateWork();
                        ff_cc.Flow_type_id = ft.Id;
                        ff_cc.Flow_type_name = ft.Flow_name;
                        foreach (Flow_NewFlow fnf in list_newflow)
                        {
                            if (fnf.Base_flow_id == ft.Id)
                            {
                                list_temp.Add(fnf);
                            }
                        }
                        ff_cc.Flow_newflows = list_temp;
                        list.Add(ff_cc);
                    }
                }
            }
            finally
            {
                if (sqlConn != null)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return list;
        }
    }
}
